package com.adnaan.restaurant.controller;

import com.adnaan.restaurant.entity.Student;
import com.adnaan.restaurant.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * author: adnaan
 * create:  2023-10-25 13:58
 * Description:
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @GetMapping("/detail")
    public Student getDetail(HttpServletRequest request) {

        String sno = request.getParameter("sno");
        String name = request.getParameter("name");
        System.out.println("学生学号为" + sno);
        System.out.println("学生姓名为" + name);
        Student student = new Student();
        student.setSno(sno);
        student.setName(name);
        return student;
    }

    @GetMapping("/detail1")
    public Student detail(String sno, String name, String sex) {

        System.out.println("学生学号为" + sno);
        System.out.println("学生姓名为" + name);
        System.out.println("学生性别为" + sex);

        Student student = new Student();
        student.setSno(sno);
        student.setName(name);
        student.setSex(sex);
        return student;
    }

    @GetMapping("detail2")
    public Student detail2(Student student) {

        System.out.println("detail2");

        return student;
    }

    @RequestMapping(value = "detail3", method = RequestMethod.GET)
    public Student detail3(@RequestParam(value = "name",
            required = false, defaultValue = "lisi") String name) {

        System.out.println("studentName" + name);

        return null;
    }

    @GetMapping("/{id}")
    public Student getInfo(@PathVariable(value = "id") Long stuId) {
        System.out.println("ID" + stuId);
        Student student = studentService.selectStudentById(stuId);
        return student;
    }

    @GetMapping("/list")
    public Student getList(Student student) {

        return student;
    }

    //@RequestBody 将前端传回来的json转为对象
    @PostMapping
    public Student addStudent(@RequestBody Student student) {

        System.out.println("新增学生信息");
        System.out.println(student);

        return student;
    }

    @PutMapping
    public Student updateStudent(@RequestBody Student student) {

        System.out.println("修改学生信息");
        System.out.println(student);

        return student;
    }

    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable(value = "id") Long id) {

        System.out.println("删除学生信息");
        System.out.println("id" + id);
    }

    @PostMapping("/batch")
    public Student[] batchAdd(@RequestBody Student[] studentArr) {
        System.out.println("新增学生信息(数组)");
        for (Student student : studentArr) {
            System.out.println(student);
        }
        return studentArr;
    }

    @PostMapping("/list")
    public List<Student> listAdd(@RequestBody List<Student> studentlist) {
        System.out.println("新增学生信息(列表)");
        for (Student student : studentlist) {
            System.out.println(student);
        }
        return studentlist;
    }
}
